package org.amphiprion.montrictracv4.web.adapter;

import java.util.List;

import org.amphiprion.montrictracv4.App;
import org.amphiprion.montrictracv4.R; 

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAndLabelListAdapter extends ArrayAdapter<ImageAndLabelListItem> {

	FragmentActivity context;
	int layoutResourceId;
	List<ImageAndLabelListItem> data = null;

	public ImageAndLabelListAdapter(FragmentActivity paramFragmentActivity, int layoutResourceId, List<ImageAndLabelListItem> data) {
		super(paramFragmentActivity, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = paramFragmentActivity;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ItemHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ItemHolder();
			holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
			holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);

			row.setTag(holder);
		} else {
			holder = (ItemHolder) row.getTag();
		}

		ImageAndLabelListItem item = data.get(position);
		holder.txtTitle.setText(item.getLabel());
		App.setUrlDrawable(holder.imgIcon, item.getImage(), context.getSupportFragmentManager());
		return row;
	}

	static class ItemHolder {
		ImageView imgIcon;
		TextView txtTitle;
	}
}
